
package com.zcp.pcsuitall.test;

import com.zcp.pcsuitall.XmlDataDealWith;

import android.test.AndroidTestCase;

import java.nio.channels.SocketChannel;
import java.util.LinkedList;

import junit.framework.Assert;

public class ContactPressTest300 extends AndroidTestCase {

    // 生成一个socketchannel
    private SocketChannel sc;

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        CommonUtil.startActivity(super.getContext());
        sc = CommonUtil.getChannel();
    }

    @Override
    protected void tearDown() throws Exception {
        // 关闭当前通道，可以测试通道的长连接
        if (!CommonUtil.closeChannel(sc)) {
            CommonUtil.stopActivity(super.getContext());
        }
        super.tearDown();
    }

    private void clearOldData() {
        // 验证是否清空
        // ids = CommonUtil.getContactsId(getAllContact());
        String cmd = "<?xml version=\"1.0\" encoding=\"utf-8\"?><root><action>CleanupContact</action><data/></root>";
        CommonUtil.sendData(sc, cmd);
        String result = CommonUtil.writeDataToFile(sc, 2000);
        Assert.assertEquals("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><action></action></root>",
                XmlDataDealWith.getXmlFormat(result));

        LinkedList<String> ids = CommonUtil.getContactsId(getAllContact());
        assertEquals(0, ids.size());

    }

    /**
     * 获取1-1000个联系人，即获取所有联系人。
     * 
     * @return
     */
    private String getAllContact() {
        // 先查询所有的联系人信息（规范中规定联系人容量是1000）。
        String getContact = "<?xml version=\"1.0\" encoding=\"utf-8\"?><root><action>LoadContact</action><data></data><from>1</from><to>1000</to></root>";
        // 发送数据
        CommonUtil.sendData(sc, getContact);
        String result = CommonUtil.writeDataToFile(sc, 2000);
        ContactsManagerTest.basicCheck(result, "LoadContact");
        return result;
    }

    /**
     * 添加联系人一次增加200条共1000。<br>
     * 
     * @throws Throwable
     */
    public void testAddContactInfoOne300() throws Throwable {
        // 测试中发送数据缓存不过，修改
        for (int i = 0; i < 3; i++) {
            addContactDifferData(100);
        }
        // int number = XmlDataDealWith.getElementNumber(getAllContact(),
        // "display_name", 5);
        //
        // Assert.assertEquals(1000, number);

        clearOldData();

    }

    /**
     * 添加联系人一次增加一条一共加1000。<br>
     * 
     * @throws Throwable
     */
    public void testAddContactInfoEvery1Total300() throws Throwable {

        for (int i = 0; i < 300; i++) {
            addContactDifferByid(1);
        }

        // int number = XmlDataDealWith.getElementNumber(getAllContact(),
        // "display_name", 5);
        //
        // Assert.assertEquals(1000, number);

        clearOldData();

    }

    /**
     * 添加联系人一次增加100条一共加1000。<br>
     * 
     * @throws Throwable
     */
    public void testAddContactInfoEvery10Total300() throws Throwable {

        for (int i = 0; i < 30; i++) {
            addContactDifferData(10);
        }

        // int number = XmlDataDealWith.getElementNumber(getAllContact(),
        // "display_name", 5);
        //
        // Assert.assertEquals(1000, number);

        clearOldData();

    }

    /**
     * 添加联系人一次增加5条一共加1000。<br>
     * 
     * @throws Throwable
     */
    public void testAddContactInfoEvery5Total300() throws Throwable {

        for (int i = 0; i < 60; i++) {
            addContactDifferData(5);
        }

        // int number = XmlDataDealWith.getElementNumber(getAllContact(),
        // "display_name", 5);
        //
        // Assert.assertEquals(1000, number);

        clearOldData();

    }

    public void testGetContactInfoEvery10Total300() throws Throwable {
        // 先查询所有的联系人信息（规范中规定联系人容量是1000）。
        addContactDifferData(300);
        String getContact = null;
        for (int i = 0; i < 30; i++) {
            getContact = "<?xml version=\"1.0\" encoding=\"utf-8\"?><root><action>LoadContact</action><data></data><from>"
                    + (i * 10 + 1) + "</from><to>" + ((i + 1) * 10) + "</to></root>";
            // 发送数据
            CommonUtil.sendData(sc, getContact);
            String result = CommonUtil.writeDataToFile(sc, 25);
            ContactsManagerTest.basicCheck(result, "LoadContact");
        }
        clearOldData();
    }

    /**
     * 增加指定数量的联系人,这批数据根据pc端测试补充。
     * 
     * @param number 要增加的数量。
     */
    private void addContactDifferData(int number) {
        if (number <= 0) {
            return;
        }

        // 添加number个联系人
        StringBuilder format = new StringBuilder();
        format.append("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><action>AddContact</action><data>");
        for (int i = 0; i < number; i++) {
            format.append("<item><id>"
                    + (i + 1)
                    + "</id><source_id></source_id><last_contact></last_contact><starred>0</starred><account_name></account_name><account_type></account_type><structuredName><display_name>郑文其"
                    + (i + 1)
                    + "好人"
                    + i
                    + "</display_name><given_name>好人"
                    + i
                    + "</given_name><family_name>郑文其"
                    + (i + 1)
                    + "</family_name><middle_name></middle_name></structuredName><nickname><item><name/></item></nickname><photo></photo><phone><item><type>2</type><number>13950487053</number><label></label><isPrimary>0</isPrimary></item><item><type>1</type><number>22779930"
                    + (i + 1)
                    + "</number><label></label><isPrimary>0</isPrimary></item><item><type>5</type><number>123456789</number><label></label><isPrimary>0</isPrimary></item><item><type>1</type><number>22779930</number><label></label><isPrimary>0</isPrimary></item><item><type>5</type><number>123456789</number><label></label><isPrimary>0</isPrimary></item></phone><email><item><data>conquore@163.com</data><type>1</type><label></label></item></email><im><item><data>908517999</data><type>3</type><label></label><protocol></protocol><custom_protocol></custom_protocol></item></im><event><item><start_date/><type/><label/></item></event><organization><item><company>工作组织</company><type>1</type><label></label><title>职务</title></item><item><company>我家</company><type>0</type><label>世界维和会</label><title>主席</title></item><item><company>工作组织</company><type>1</type><label></label><title>职务</title></item><item><company>我家</company><type>0</type><label>世界维和会</label><title>主席</title></item><item><company>中兴</company><type>1</type><label></label><title>员工</title></item></organization><groupMember><item><groupID/></item></groupMember><structuredPostal><item><formatted_address/><type/><label/><street/><pobox/><neighborhood/><city/><region/><postcode/><country/></item></structuredPostal><relation><item><name/><type/><label/></item></relation><SipAddress><item><sip_address/><type/><label/></item></SipAddress><website><item><url/><type/><label/></item></website><note>朋友</note></item>");
        }
        format.append("</data></root>");
        // 增加数据
        String addContactInfo = format.toString();
        // Log.i(TAG, addContactInfo);
        // 发送数据
        CommonUtil.sendData(sc, addContactInfo);
        String addresult = CommonUtil.writeDataToFile(sc, number * 2 <= 25 ? 25 : number * 2);
        ContactsManagerTest.basicAddCheck(addresult);
        format = null;
        addContactInfo = null;
        addresult = null;

    }

    /**
     * 增加指定数量的联系人,这批数据根据pc端测试补充。
     * 
     * @param number 要增加的数量。
     */
    private void addContactDifferByid(int i) {

        // 添加number个联系人
        StringBuilder format = new StringBuilder();
        format.append("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><action>AddContact</action><data>");
        format.append("<item><id>"
                + (i + 1)
                + "</id><source_id></source_id><last_contact></last_contact><starred>0</starred><account_name></account_name><account_type></account_type><structuredName><display_name>郑文其"
                + (i + 1)
                + "好人"
                + i
                + "</display_name><given_name>好人"
                + i
                + "</given_name><family_name>郑文其"
                + (i + 1)
                + "</family_name><middle_name></middle_name></structuredName><nickname><item><name/></item></nickname><photo></photo><phone><item><type>2</type><number>13950487053"
                + (i + 1)
                + "</number><label></label><isPrimary>0</isPrimary></item><item><type>1</type><number>22779930</number><label></label><isPrimary>0</isPrimary></item><item><type>5</type><number>123456789</number><label></label><isPrimary>0</isPrimary></item><item><type>1</type><number>22779930</number><label></label><isPrimary>0</isPrimary></item><item><type>5</type><number>123456789</number><label></label><isPrimary>0</isPrimary></item></phone><email><item><data>conquore@163.com</data><type>1</type><label></label></item></email><im><item><data>908517999</data><type>3</type><label></label><protocol></protocol><custom_protocol></custom_protocol></item></im><event><item><start_date/><type/><label/></item></event><organization><item><company>工作组织</company><type>1</type><label></label><title>职务</title></item><item><company>我家</company><type>0</type><label>世界维和会</label><title>主席</title></item><item><company>工作组织</company><type>1</type><label></label><title>职务</title></item><item><company>我家</company><type>0</type><label>世界维和会</label><title>主席</title></item><item><company>中兴</company><type>1</type><label></label><title>员工</title></item></organization><groupMember><item><groupID/></item></groupMember><structuredPostal><item><formatted_address/><type/><label/><street/><pobox/><neighborhood/><city/><region/><postcode/><country/></item></structuredPostal><relation><item><name/><type/><label/></item></relation><SipAddress><item><sip_address/><type/><label/></item></SipAddress><website><item><url/><type/><label/></item></website><note>朋友</note></item>");
        format.append("</data></root>");
        // 增加数据
        String addContactInfo = format.toString();
        // Log.i(TAG, addContactInfo);
        // 发送数据
        CommonUtil.sendData(sc, addContactInfo);
        String addresult = CommonUtil.writeDataToFile(sc, 25);
        ContactsManagerTest.basicAddCheck(addresult);
        format = null;
        addContactInfo = null;
        addresult = null;

    }
}
